<template>
  <a-card :bordered="false">

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus"> {{$t('base.add')}} </a-button>
      <a-button
        @click="batchDel"
        v-if="selectedRowKeys.length > 0"
        ghost
        type="primary"
        icon="delete"> {{$t('base.batchDel')}}
      </a-button>
    </div>

    <!-- table区域-begin -->
    <div>

      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>{{$t('base.selected')}}&nbsp;<a style="font-weight: 600">{{selectedRowKeys.length }}</a>{{$t('base.item')}}&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected"> {{$t('base.clear')}} </a>
      </div>

      <a-table
        :columns="columns"
        :scroll="{x: 1500}"
        size="middle"
        :pagination="false"
        :dataSource="dataSource"
        :loading="loading"
        :expandedRowKeys="expandedRowKeys"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @expandedRowsChange="handleExpandedRowsChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)"> {{$t('base.edit')}} </a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">
              {{$t('base.more')}} <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)"> {{$t('base.detailL')}} </a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="handleAddSub(record)"> {{$t('systemManagement.addSub')}} </a>
              </a-menu-item>
              <a-menu-item>
                <a href="javascript:;" @click="handleDataRule(record)"> {{$t('systemManagement.dataRule')}} </a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm :title="$t('base.confirmDelete')" @confirm="() => handleDelete(record.id)">
                  <a> {{$t('base.del')}} </a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
        <!-- 字符串超长截取省略号显示 -->
        <span slot="url" slot-scope="text">
          <j-ellipsis :value="text" :length="25"/>
        </span>
        <!-- 字符串超长截取省略号显示-->
        <span slot="component" slot-scope="text">
          <j-ellipsis :value="text"/>
        </span>

        <span slot="menuType" slot-scope="text, record">
          <span v-show="record.menuType==0">{{$t('systemManagement.menu')}}</span>
          <span v-show="record.menuType==1">{{$t('systemManagement.menu')}}</span>
          <span v-show="record.menuType==2">{{$t('systemManagement.buttonsOrPermissions')}}</span>
        </span>


      </a-table>

    </div>
    <!-- table区域-end -->

    <permission-modal ref="modalForm" @ok="modalFormOk"></permission-modal>
    <permission-data-rule-list ref="PermissionDataRuleList" @ok="modalFormOk"></permission-data-rule-list>

  </a-card>
</template>

<script>
  import PermissionModal from './modules/PermissionModal'
  import { getPermissionList } from '@/api/api'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import PermissionDataRuleList from './PermissionDataRuleList'
  import JEllipsis from '@/components/jeecg/JEllipsis'


  export default {
    name: 'PermissionList',
    mixins: [JeecgListMixin],
    components: {
      PermissionDataRuleList,
      PermissionModal,
      JEllipsis
    },
    data() {
      return {
        description: this.$t('systemManagement.menu'),
        // 表头
        columns: [
          {
            title:  this.$t('systemManagement.menuName'),
            dataIndex: 'name',
            key: 'name'
          },
          {
            title:  this.$t('systemManagement.menuType') ,
            align:"center",
            scopedSlots: { customRender: 'menuType' }
          },
          /*{
            title: '权限编码',
            dataIndex: 'perms',
            key: 'permissionCode',
          },*/
          {
            title: this.$t('systemManagement.icon'),
            dataIndex: 'icon',
            key: 'icon'
          },
          {
            title: this.$t('systemManagement.assembly'),
            dataIndex: 'component',
            key: 'component',
            scopedSlots: { customRender: 'component' }
          },
          {
            title:  this.$t('systemManagement.route') ,
            dataIndex: 'url',
            key: 'url',
            scopedSlots: { customRender: 'url' }
          },
          {
            title:  this.$t('systemManagement.sort') ,
            dataIndex: 'sortNo',
            key: 'sortNo'
          },
          {
            title: this.$t('base.operation'),
            dataIndex: 'action',
            fixed: 'right',
            scopedSlots: { customRender: 'action' },
            align: 'center',
            width: 150
          }
        ],
        loading: false,
        // 展开的行，受控属性
        expandedRowKeys: [],
        url: {
          list: '/sys/permission/list',
          delete: '/sys/permission/delete',
          deleteBatch: '/sys/permission/deleteBatch'
        }
      }
    },
    methods: {
      loadData() {
        this.dataSource = []
        getPermissionList().then((res) => {
          if (res.success) {
            this.dataSource = res.result
          }
        })
      },
      // 打开数据规则编辑
      handleDataRule(record) {
        this.$refs.PermissionDataRuleList.edit(record)
      },
      handleAddSub(record) {
        this.$refs.modalForm.title =  this.$t('systemManagement.addSubMenu') ;
        this.$refs.modalForm.localMenuType = 1;
        this.$refs.modalForm.disableSubmit = false;
        this.$refs.modalForm.edit({status:'1',permsType:'1',route:true,'parentId':record.id});
      },
      handleExpandedRowsChange(expandedRows) {
        this.expandedRowKeys = expandedRows
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>